package de.webtech.faceplace.components;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.tapestry5.*;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Response;
import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.SymbolConstants;
import org.hibernate.Session;

import de.webtech.faceplace.entities.Person;
import de.webtech.faceplace.pages.Welcome;


/**
 * Layout component for pages of application faceplace.
 */
@Import(stylesheet = "context:layout/layout.css")
public class Layout
{
    /**
     * The page title, for the <title> element and the <h1> element.
     */
    @Property
    @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
    private String title;

    @Property
    private String pageName;

    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private String sidebarTitle;

    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private Block sidebar;
    
    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private Block sleft;
    
    @Property
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private String menuTitle;
    
    

    @Inject
    private ComponentResources resources;

    @Property
    @Inject
    @Symbol(SymbolConstants.APPLICATION_VERSION)
    private String appVersion;
    


    public boolean getClassForPageName()
    {
        return resources.getPageName().equalsIgnoreCase(pageName)
                ? true
                : false;
    }

    public String[] getPageNames()
    {
        return new String[]{"Index", "Profile", "About", "Contact"};
    }
    @Inject
	private Request request;
    
    public Person getPerson(){
        Person p = (Person) request.getSession(true).getAttribute("user");
       return p;	
       }
       
       public String getFirstName(){
       	return getPerson().firstName;
       }
       public String getLastName(){
       	return getPerson().lastName;
       }
       public Object onActionFromLogout() {

       	request.getSession(true).setAttribute("user", null);
       	return Welcome.class;
       }


       
       //TODO falls befreundet auch LastName anzeigen
       @Property
   	@SuppressWarnings("unused")
   	private String userName;
   	
   	List<String> onProvideCompletionsFromUserName(String partial) {
   		
   		List<String> matches = new ArrayList<String>();
   		
   		for (Object userName : session.createCriteria(Person.class).list()) {
   			if (((Person) userName).firstName.toLowerCase().startsWith(partial.toLowerCase()) && (((Person) userName).id!=((Person) request.getSession(true).getAttribute("user")).id)) {
   				
   				String userMatch = ((Person) userName).firstName + " "+ ((Person) userName).lastName + " - [" + ((Person) userName).id + "]";
   				
   			
   				matches.add(userMatch);
   				
   			}
   		}

   		return matches;
   	}
   	
   	//TODO referenz für ausgewälte person mitgeben... 
   	@Inject
   	Response response;
   	public void onSubmitFromForm() throws IOException{
   		try{
   		response.sendRedirect(("profile?uid=" +cutBack(cutFront(userName, "[", 1), "]", 1)));
   		}
   		catch(Exception e){
   			
   		}
   		
   		
   	}
   	
   	public String cutFront(String txt, String teil, int num) {
        for (int i = 0; i < num; i++) {
            txt = txt.substring(txt.indexOf(teil) + 1, txt.length());
        }
        return txt;
    } 
   	
   	public String cutBack(String txt, String teil, int num) {
        for (int i = 0; i < num; i++) {
            txt = txt.substring(0, txt.lastIndexOf(teil));
        }
        return txt;
    } 
;
   	@Inject
    private Session session;
   	
    public String getPhotoSource(){
 	   if(getPerson().photoSource!=null){
 	   return getPerson().photoSource;
    }
 	   else{
 		   return "layout/profilepictures/dummy.jpg";
 	   }
    


}
    
}
